﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace MixScript.BLL.FI
{
    public class cMappingMaterial : cBaseBLL
    {

        public DataTable List()
        {

            try
            {

                string sSQL = @"SELECT DISTINCT bpidnm ModulName,Maping.FK_ModulID  accountid ,isnull(ac.acctname ,'') acctname   FROM
                                    vw_joid_module_v M 
                                    inner join vw_joid_bill_material_v bom on M.codfbpid  =bom.paitmfbp 
                                    inner join tb_MappingModulMaterial Maping ON bom.itno = Maping.FK_MaterialID 
                                    inner join accaccounts ac on ac.acctnum = FK_ModulID  
                                    ";

                return Dbserver.ExecuteQuery(sSQL, "vw_coamst");



            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        public void Mapped(String modulId, string acctnum)
        {
            try
            {
                Dbserver.openConn();
                string sSQL = @"IF EXISTS(SELECT DISTINCT bpidnm ModulName,Maping.FK_ModulID   ,isnull(bpidnm ,'') acctname   FROM
                                vw_joid_module_v M 
                                inner join vw_joid_bill_material_v bom on M.codfbpid  =bom.paitmfbp 
                                inner join tb_MappingModulMaterial Maping ON bom.itno = Maping.FK_MaterialID where M.codfbpid  ='{0}')
                                update Maping  SET Maping.FK_ModulID = '{1}'  from  vw_joid_module_v M 
                                inner join vw_joid_bill_material_v bom on M.codfbpid  =bom.paitmfbp 
                                inner join tb_MappingModulMaterial Maping ON bom.itno = Maping.FK_MaterialID where M.codfbpid  ='{0}'
                                else
                                insert into tb_MappingModulMaterial
                                SELECT '{1}',bom.itno  from  vw_joid_module_v M 
                                inner join vw_joid_bill_material_v bom on M.codfbpid  =bom.paitmfbp 
                                where M.codfbpid  ='{0}'
";
                Dbserver.ExecuteNonQuery(String.Format(sSQL, modulId, acctnum));
                Dbserver.CommitAndCloseConn();
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
